import java.util.Stack;

public class Sort {
    public static void insertSort(int[] array){
        int n = array.length;

        for(int i = 1; i < n; i++){
            int j = i - 1;
            int tmp = array[i];
            for( ; j >= 0; j--){
                if(tmp < array[j]){
                    array[j + 1] = array[j];
                }else{
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    public static void shellSort(int[] array){
        int gap = array.length;
        while(gap > 0){
            gap /= 2;
            shell(array, gap);
        }
    }

    private static void shell(int[] array, int gap){
        int n = array.length;
        for(int i = gap; i < n; i++){
            int tmp = array[i];
            int j = i - gap;
            for(; j >= 0; j -= gap){
                if(array[j] > tmp){
                    array[j + gap] = array[j];
                }else{
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    public static void selectSort(int[] array){
        int n = array.length;
        for(int i = 0; i < n; i++){
            int minIndex = i;
            for(int j = i + 1; j < n; j++){
                if(array[j] < array[minIndex]){
                    minIndex = j;
                }
            }
            swap(array, i, minIndex);
        }
    }

    private static void swap(int[] array, int i, int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public static void heapSort(int[] array){
        // 建一个大根堆
        int n = array.length;
        int parent = (n - 1 - 1) / 2;
        for( ; parent >= 0; parent--){
            shiftDown(array, parent, n);
        }
        // 排序
        int end = n - 1;
        while(end > 0){
            swap(array, 0, end);
            shiftDown(array, 0, end--);
        }
    }

    private static void shiftDown(int[] array, int parent, int size){
        int child = parent * 2 + 1;
        while(child < size){
            if(child + 1 < size && array[child] < array[child + 1]){
                child++;
            }
            if(array[child] > array[parent]){
                swap(array, child, parent);
                parent = child;
                child = parent * 2 + 1;
            }else{
                break;
            }
        }
    }

    public static void bubbleSort(int[] array){
        int n = array.length;
        for(int i = 0; i < n - 1; i++){
            boolean flag = false;
            for(int j = 0; j < n - 1 - i; j++){
                if(array[j] > array[j + 1]){
                    swap(array, j, j + 1);
                    flag = true;
                }
            }
            if(!flag) break;
        }
    }

    public static void quickSort(int[] array){
        int n = array.length;
        quick(array, 0, n - 1);
    }

    public static void quick(int[] array, int left, int right){
        if(left >= right) return;

        int index = midOfThree(array, left, right);
        swap(array, index, left);
        //int pivot = partition(array, left, right);
        int pivot = partition2(array, left, right);

        quick(array, left, pivot - 1);
        quick(array, pivot + 1, right);
    }

    private static int midOfThree(int[] array, int left, int right){
        int mid = (left + right) / 2;
        if(array[left] < array[right]){
            if(array[mid] < array[left]) return left;
            else if(array[mid] > array[right]) return right;
            else return mid;
        }else {
            if(array[mid] < array[right]) return right;
            else if(array[mid] > array[left]) return left;
            else return mid;
        }
    }

    // Hoare
    private static int partition(int[] array, int left, int right){
        int tmp = array[left];
        int pos = left;

        while(left < right){
            while(left < right && array[right] >= tmp) right--;
            while(left < right && array[left] <= tmp) left++;
            swap(array, left, right);
        }
        swap(array, pos, left);

        return left;
    }

    // 挖坑法
    private static int partition2(int[] array, int left, int right){
        int tmp = array[left];
        while(left < right) {
            while(left < right && array[right] >= tmp) right--;
            array[left] = array[right];
            while(left < right && array[left] <= tmp) left++;
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    // 快排的非递归版本
    public static void quickSortNor(int[] array){
        int n = array.length;
        Stack<Integer> stack = new Stack<>();

        stack.push(0);
        stack.push(n - 1);

        while(!stack.isEmpty()){
            int right = stack.pop();
            int left = stack.pop();

            int pivot = partition2(array, left, right);

            if(left + 1 < pivot){
                stack.push(left);
                stack.push(pivot - 1);
            }
            if(pivot + 1 < right){
                stack.push(pivot + 1);
                stack.push(right);
            }
        }
    }

    public static void mergeSort(int[] array){
        mergeSortFunc(array, 0, array.length - 1);
    }

    private static void mergeSortFunc(int[] array, int left, int right) {
        if(left >= right) return;

        int mid = (right + left) / 2;
        mergeSortFunc(array, left, mid);
        mergeSortFunc(array, mid + 1, right);

        merge(array, left, mid, right);
    }

    private static void merge(int[] array, int left, int mid, int right){
        int i = left;
        int j = mid + 1;
        int k = 0;
        int[] tmp = new int[right - left + 1];

        while(i <= mid && j <= right){
            if(array[i] <= array[j]){
                tmp[k++] = array[i++];
            }else{
                tmp[k++] = array[j++];
            }
        }

        while(i <= mid) tmp[k++] = array[i++];
        while(j <= right) tmp[k++] = array[j++];

        for(int l = left; l <= right; l++){
            array[l] = tmp[l - left];
        }
    }

    public static void mergeSortNor(int[] array){
        int n = array.length;
        int gap = 1;

        while(gap < n){
            for(int i = 0; i < n; i += 2 * gap){
                int left = i;
                int mid = i + gap - 1;
                int right = mid + gap;

                if(mid >= n) mid = n - 1;
                if(right >= n) right = n - 1;

                merge(array, left, mid, right);
            }
            gap *= 2;
        }
    }

    public static void countSort(int[] array){
        int n = array.length;
        int minVal = array[0];
        int maxVal = array[0];

        for(int x : array){
            if(minVal > x) minVal = x;
            if(maxVal < x) maxVal = x;
        }

        int[] count = new int[maxVal - minVal + 1];

        for(int x : array){
            count[x - minVal]++;
        }

        int pos = 0;
        for(int i = 0; i < count.length; i++){
            while(count[i]-- > 0){
                array[pos++] = i + minVal;
            }
        }
    }
}
